﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace NLBS.Urls
{
    /// <summary>
    /// Represents a single Url pattern and rewritten path
    /// </summary>
    public class ReWrittenUrl
    {
        private string _name;
        private string _path;
        private string _pattern;
        private Regex _regex = null;

        private string _defaultPage;

        /// <summary>
        /// Instantiates a new ReWrittenUrl
        /// </summary>
        /// <param name="name">Name of this RewrittenUrl</param>
        /// <param name="pattern">Regex pattern used to match urls to be rewritten by this ReWrittenUrl</param>
        /// <param name="path">Rewritten path containing formatting placeholders for parameters</param>
        /// <param name="defaultPage">Default name of the physical page represented by this ReWrittenUrl</param>
        /// <param name="redirect">Is this a redirected link</param>
        public ReWrittenUrl(string name, string pattern, string path, string defaultPage, bool redirect)
        {
            _name = name;
            _path = path;
            _pattern = "^" + pattern;

            _defaultPage = defaultPage;
            _isRedirect = redirect;

            _regex = new Regex(_pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
        }

        /// <summary>
        /// Returns true if the specified url path matches this ReWrittenUrl
        /// </summary>
        /// <param name="url">Url path to test</param>
        /// <returns>True if the url path matches this ReWrittenUrl</returns>
        public bool IsMatch(string url)
        {
            bool a = _regex.IsMatch(url);
            return _regex.IsMatch(url);
        }

        /// <summary>
        /// Returns the named value from the specified url
        /// </summary>
        /// <param name="url">Url path to retrieve the named value from</param>
        /// <param name="name">Name of the value to retrieve from the specified Url path</param>
        /// <returns>Value matching the given name, or null if the value could not be found</returns>
        public string NamedValue(string url, string name)
        {
            return _regex.Match(url).Groups[name].Value;
        }

        /// <summary>
        /// The Regex pattern used to match this ReWrittenUrl to a given Url path
        /// </summary>
        public string Pattern
        {
            get { return _pattern; }
        }

        /// <summary>
        /// The name of this ReWrittenUrl
        /// </summary>
        public string Name
        {
            get { return _name; }
        }

        /// <summary>
        /// Rewritten path containing formatting placeholders for parameters
        /// </summary>
        public string Path
        {
            get { return _path; }
        }

        private bool _isRedirect = false;
        public bool IsRedirect
        {
            get { return _isRedirect; }
        }

        /// <summary>
        /// Parses the given Url path and returns the rewritten url for the given ThemePage
        /// </summary>
        /// <param name="url">Original Url path to rewrite</param>
        /// <param name="qs">Original querystring to rewrite</param>
        /// <param name="tp">ThemePage used to process this request</param>
        /// <returns>Rewritten Url for the given Url path, query string, and ThemePage</returns>
        public virtual string GetReWrittenUrl(string url, string qs, ThemePage tp)
        {
            string pageName = _defaultPage;

            if (tp != null)
                if (!string.IsNullOrEmpty(tp.Page))
                    pageName = tp.Page;

            string path = string.Format(_path, pageName);
            //can we add {0}{1}{2} and drop 2 from the config?

            url = _regex.Replace(url, path);

            if (qs != null && qs.StartsWith("?") && url.Contains("?"))
            {
                qs = qs.Replace("?", "&");
            }

            return string.Format("{0}{1}", url, qs);
        }
    }
}
